package RenRen;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

/**
 * 利用httpclient操作人人网 比如登陆，发状态，模拟你访问任何人主页等等
 * http://liubey.javaeye.com/
 * @author EasonLau
 */
public class RenRenStatus3 {
	// 人人网账号
	private String userName = "";
	// 人人网密码
	private String password = "";

	private static String redirectURL = "http://www.renren.com/home";

	// Don't change the following URL
	private static String renRenLoginURL = "http://www.renren.com/PLogin.do";

	// The HttpClient is used in one session
	private HttpResponse response;
	private DefaultHttpClient httpclient = null;

	public RenRenStatus3(String userName, String password) {
		this.userName = userName;
		this.password = password;
	}

	/**
	 * 登陆
	 * @return
	 */
	private boolean login() {
		if (httpclient != null) {
			System.out.println("登陆成功");
			return true;
		}
		httpclient = null;
		httpclient = new DefaultHttpClient();
		HttpPost httpost = new HttpPost(renRenLoginURL);
		// All the parameters post to the web site
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("origURL", redirectURL));
		nvps.add(new BasicNameValuePair("domain", "renren.com"));
		nvps.add(new BasicNameValuePair("autoLogin", "true"));
		nvps.add(new BasicNameValuePair("formName", ""));
		nvps.add(new BasicNameValuePair("method", ""));
		nvps.add(new BasicNameValuePair("submit", "登录"));
		nvps.add(new BasicNameValuePair("email", userName));
		nvps.add(new BasicNameValuePair("password", password));
		try {
			httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
			response = httpclient.execute(httpost);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			httpost.abort();
		}

		String redirectLocation = getRedirectLocation();
		if (redirectLocation != null) {
			// System.out.println(getText(redirectLocation));
			// 跳到首页，现在登录完成
			getText(redirectLocation);
		}
		System.out.println(redirectLocation);

		return true;
	}

	private String getRedirectLocation() {
		Header locationHeader = response.getFirstHeader("Location");
		if (locationHeader == null) {
			return null;
		}
		return locationHeader.getValue();
	}

	private String getText(String redirectLocation) {
		HttpGet httpget = new HttpGet(redirectLocation);
		// Create a response handler
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String responseBody = "";
		try {
			responseBody = httpclient.execute(httpget, responseHandler);
		} catch (Exception e) {
			e.printStackTrace();
			responseBody = null;
		} finally {
			httpget.abort();
			// httpclient.getConnectionManager().shutdown();
		}
		return responseBody;
	}

	/**
	 * 更新本人状态
	 * @return
	 */
	private boolean update() {
		if (login()) {

			HttpPost post = new HttpPost(
					"http://status.renren.com/doing/update.do?");
			List<NameValuePair> cp = new ArrayList<NameValuePair>();
			cp.add(new BasicNameValuePair("c", "这里发布你的状态"));
			cp.add(new BasicNameValuePair("isAtHome", "1"));
//			cp.add(new BasicNameValuePair("publisher_form_ticket","每个人都不一样，需要抓取"));
//			cp.add(new BasicNameValuePair("raw", "这里同c一样"));
//			cp.add(new BasicNameValuePair("requestToken", "每个人都不一样，需要抓取2"));
			cp.add(new BasicNameValuePair("statusPage", "1"));
			try {
				post.setEntity(new UrlEncodedFormEntity(cp, HTTP.UTF_8));
				response = httpclient.execute(post);

				System.out.println(response.getStatusLine());

			} catch (Exception e) {
				e.printStackTrace();
				return false;
			} finally {
				post.abort();
			}
			return true;
		}
		return false;
	}

	/**
	 * 访问
	 * @param id
	 * @return
	 */
	private boolean visit(int id) {

			HttpPost post = new HttpPost("http://www.renren.com/profile.do?portal=profileFootprint&ref=profile_footprint&id=" + id);
			try {
				response = httpclient.execute(post);
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			System.out.println("访问成功！id号：" + id);
		post.abort();
		return true;
	}

	/**
	 * 给好友留言 没有实现 请牛人帮忙实现
	 * @param id 好友ID
	 * @return
	 */
	private boolean message(int id) {

		return true;
	}

	/**
	 * 取到你所有的好友ID
	 * @return
	 */
	private int getFriends() {
		int id = 0;

		return id;
	}

	/**
	 * main方法
	 * @param args
	 */
	public static void main(String[] args) {

		RenRenStatus3 lw = null;
		int id = 238393985;//从此ID开始循环
		int id2 = 238393985;//循环到此ID
		lw = new RenRenStatus3("zhangfy068@163.com", "529595068");
		lw.login();
		lw.update();
		/**
		 * 以下写的相当人人网乱，实现的功能就是遍历你输入的俩ID之间的所有人，然后用你去访问这些人的页面
		 */
		int sign = 0;//当前循环次数
		int goon = 0;//取得对话框返回值
		for(int i = id;i<=id2;i++) {
			if(sign >=100) {
				if(sign % 100 == 0) {
					goon = JOptionPane.showConfirmDialog(null, "请随意选一个好友访问，并输入提示的验证码以便继续访问！");
				}
			}

			if(goon == 0) {
				lw.visit(i);
			}
				sign++;
			}

		}
}